using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq.Expressions;
using System.Windows.Forms;
using System.Windows.Input;

namespace ConverterSample
{
    public class ViewModel : INotifyPropertyChanged
    {
        #region fields

        private Image _image;
        private ICommand _fileCommand;

        #endregion

        #region properties

        public Image SourceImage
        {
            get { return _image; }
            set
            {
                _image = value;
                OnPropertyChanged(() => SourceImage);
            }
        }

        public ICommand FileCommand
        {
            get { return _fileCommand ?? (_fileCommand = new Actionommand(FileCommandAction)); }
        }

        #endregion

        #region ctor

        public ViewModel()
        {

        }

        #endregion

        #region private methods

        private void FileCommandAction()
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.CheckFileExists = true;
            fileDialog.Filter = "Image files (*.jpg, *.png)|*.jpg;*.png;*.jpeg";
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = fileDialog.FileName;
                SourceImage = Image.FromFile(fileName);
            }
        }

        #endregion

        #region INotifyPropertyChanged impl

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged<T>(Expression<Func<T>> expression)
        {
            MemberExpression member = expression.Body as MemberExpression;
            if (member != null)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(member.Member.Name));
            }
        }

        #endregion
    }
}